package com.waimai.mobile.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.waimai.mobile.R;
import com.waimai.mobile.api.entity.Restaurant;
import com.waimai.mobile.misc.ImageCacheLoader;
import com.waimai.mobile.misc.ImageLoader;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;

import java.util.List;

/**
 * Created by lin on 2015/5/30.
 */

@EBean
public class RestaurantItemAdpater extends RecyclerView.Adapter<RestaurantItemAdpater.ViewHolder>{

    @RootContext
    public Context context;

    private List<Restaurant> restaurants;

    public List<Restaurant> getRestaurants() {
        return restaurants;
    }

    public void setRestaurants(List<Restaurant> restaurants) {
        this.restaurants = restaurants;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_list_restaurant,viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.restNameTv.setText(restaurants.get(i).getRestaurantName());
        viewHolder.starLevelArgRb.setRating(restaurants.get(i).getStarLevelArg());
        viewHolder.monthSalesTv.setText(restaurants.get(i).getMonthSales() + "");
        viewHolder.sendPriceTv.setText(restaurants.get(i).getSendPrice()+"");
        viewHolder.deliveryPriceTv.setText(restaurants.get(i).getDeliveryPrice()+"");
        viewHolder.deliveryTimeArgTv.setText(restaurants.get(i).getDeliveryTimeArg() + "");

        loadImage(restaurants.get(i).getPhoto(), viewHolder.restPhotoIv);

        if(restaurants.get(i).getRest()){
            viewHolder.restLL.setVisibility(LinearLayout.VISIBLE);
        }else{
            viewHolder.restLL.setVisibility(LinearLayout.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return restaurants.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView restPhotoIv;
        public TextView restNameTv;
        public RatingBar starLevelArgRb;
        public TextView monthSalesTv;
        public TextView sendPriceTv;
        public TextView deliveryPriceTv;
        public TextView deliveryTimeArgTv;
        public LinearLayout restLL;

        public View view;

        public ViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            restPhotoIv = (ImageView) itemView.findViewById(R.id.restPhotoIv);
            restNameTv = (TextView) itemView.findViewById(R.id.restNameTv);
            starLevelArgRb = (RatingBar) itemView.findViewById(R.id.starLevelArgRb);
            monthSalesTv = (TextView) itemView.findViewById(R.id.monthSalesTv);
            sendPriceTv = (TextView) itemView.findViewById(R.id.sendPriceTv);
            deliveryPriceTv = (TextView) itemView.findViewById(R.id.deliveryPriceTv);
            deliveryTimeArgTv = (TextView) itemView.findViewById(R.id.deliveryTimeArgTv);
            restLL = (LinearLayout) itemView.findViewById(R.id.restLL);
        }
    }

    public void loadImage(String imageUrl, ImageView view){
        ImageLoader.load(imageUrl, view);
    }
}
